home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-gdata / atom / client.py < prev    next >
Encoding:
Python Source  |  2009-01-14  |  4.0 KB  |  112 lines

  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2009 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. #      http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16.  
  17.  
  18. """AtomPubClient provides CRUD ops. in line with the Atom Publishing Protocol.
  19.  
  20. """
  21.  
  22. __author__ = 'j.s@google.com (Jeff Scudder)'
  23.  
  24.  
  25. import atom.http_core
  26.  
  27.  
  28. class AtomPubClient(object):
  29.   host = None
  30.   auth_token = None
  31.  
  32.   def __init__(self, http_client=None, host=None, auth_token=None, **kwargs):
  33.     self.http_client = http_client or atom.http_core.HttpClient()
  34.     if host is not None:
  35.       self.host = host
  36.     if auth_token is not None:
  37.       self.auth_token = auth_token
  38.  
  39.   def request(self, method=None, uri=None, auth_token=None,
  40.               http_request=None, **kwargs):
  41.     """Performs an HTTP request to the server indicated.
  42.  
  43.     Uses the http_client instance to make the request.
  44.  
  45.     Args:
  46.       method: The HTTP method as a string, usually one of 'GET', 'POST',
  47.               'PUT', or 'DELETE'
  48.       uri: The URI desired as a string or atom.http_core.Uri. 
  49.       http_request: 
  50.       auth_token: An authorization token object whose modify_request method
  51.                   sets the HTTP Authorization header.
  52.     """
  53.     if http_request is None:
  54.       http_request = atom.http_core.HttpRequest()
  55.     # If the http_request didn't specify the target host, use the client's
  56.     # default host (if set).
  57.     if self.host is not None and http_request.host is None:
  58.       http_request.host = self.host
  59.     # Modify the request based on the AtomPubClient settings and parameters
  60.     # passed in to the request.
  61.     if isinstance(uri, (str, unicode)):
  62.       uri = atom.http_core.parse_uri(uri)
  63.     if uri is not None:
  64.       uri.modify_request(http_request)
  65.     if isinstance(method, (str, unicode)):
  66.       http_request.method = method
  67.     # Any unrecognized arguments are assumed to be capable of modifying the
  68.     # HTTP request.
  69.     for name, value in kwargs.iteritems():
  70.       if value is not None:
  71.         value.modify_request(http_request)
  72.     # Default to an http request if the protocol scheme is not set.
  73.     if http_request.scheme is None:
  74.       http_request.scheme = 'http'
  75.     # Add the Authorization header at the very end. The Authorization header
  76.     # value may need to be calculated using information in the request.
  77.     if auth_token:
  78.       auth_token.modify_request(http_request)
  79.     elif self.auth_token:
  80.       self.auth_token.modify_request(http_request)
  81.     # Perform the fully specified request using the http_client instance. 
  82.     # Sends the request to the server and returns the server's response.
  83.     return self.http_client.request(http_request)
  84.  
  85.   Request = request
  86.  
  87.   def get(self, uri=None, auth_token=None, http_request=None, **kwargs):
  88.     return self.request(method='GET', uri=uri, auth_token=auth_token, 
  89.                         http_request=http_request, **kwargs)
  90.  
  91.   Get = get
  92.  
  93.   def post(self, uri=None, data=None, auth_token=None, http_request=None, 
  94.            **kwargs):
  95.     return self.request(method='POST', uri=uri, auth_token=auth_token, 
  96.                         http_request=http_request, data=data, **kwargs)
  97.  
  98.   Post = post
  99.  
  100.   def put(self, uri=None, data=None, auth_token=None, http_request=None, 
  101.           **kwargs):
  102.     return self.request(method='PUT', uri=uri, auth_token=auth_token, 
  103.                         http_request=http_request, data=data, **kwargs)
  104.  
  105.   Put = put
  106.  
  107.   def delete(self, uri=None, auth_token=None, http_request=None, **kwargs):
  108.     return self.request(method='DELETE', uri=uri, auth_token=auth_token, 
  109.                         http_request=http_request, **kwargs)
  110.  
  111.   Delete = delete
  112.